<template>
    <el-form ref="newForm" :model="filterForm" :label-width="120 + 'px'">
        <el-form-item
            label="时间范围："
            prop="startTime"
            :rules="[{ required: true, message: '请选择时间范围' }]"
        >
            <el-date-picker
                v-model="dateRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                @change="onDateChange"
            >
            </el-date-picker>
        </el-form-item>
        <el-form-item
            label="周期："
            prop="cycle"
            :rules="[{ required: true, message: '请选择周期' }]"
        >
            <el-select v-model="filterForm.cycle" placeholder="请选择" clearable>
                <el-option
                    :label="item"
                    :value="item"
                    v-for="(item, key) in cycleList"
                    :key="key"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" :loading="loading" @click="submitAction">下载</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
export default {
    props: {
        winH: {
            type: Number,
            default: 500,
        },
        businessId: {
            //企业ID
            type: String,
            default: "",
        },
    },
    data() {
        return {
            filterForm: {
                startTime: "",
                endTime: "",
                cycle: "",
                businessId: "",
            },
            dateRange: [],
            cycleList: ["月", "季", "半年", "年", "特殊"],
            loading: false,
        }
    },
    created() {
        const now = new Date()
        this.filterForm.startTime = now.Format("yyyy-MM-dd")
        this.filterForm.endTime = now.Format("yyyy-MM-dd")
    },
    methods: {
        onDateChange(e) {
            this.filterForm.startTime = e[0]
            this.filterForm.endTime = e[1]
        },

        submitAction() {
            this.$refs["newForm"].validate((valid) => {
                if (valid) {
                    this.loading = true
                    this.filterForm.businessId = this.businessId
                    this.$get("/check/maintenance/reportExport", this.filterForm).then(
                        (res) => {
                            this.loading = false
                            const a_link = document.createElement("a")
                            a_link.href = res.data.filePath
                            document.body.appendChild(a_link)
                            a_link.click()
                            this.$emit("success")
                        },
                        (error) => {
                            this.loading = false
                            this.$message.error(error.msg)
                            this.$emit("fail")
                        }
                    )
                } else {
                    return false
                }
            })
        },
    },
}
</script>
